<?php

namespace Home\Controller;

use Home\Model\ClassModel;
use Home\Model\SigninModel;
use Home\Model\UserModel;
use Think\Controller;
use Think\Db;
use Think\Model;

class ClassController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        if ($this->user['role'] == 3) {
            $this->retError('无权限');
        }
    }

    public function index()
    {
        $choose = M('ChooseClass'); // 实例化User对象
        $class = new ClassModel(); // 实例化User对象
        $query = $class;
        if ($this->user['role'] == 1) {
            $query = $query->where('teacher_id', $this->user['id']);
        }
        $count = $query->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $query->order('create_time')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $user = new UserModel();
        foreach ($list as $key => $item) {
            $item['teacher'] = $user->where('id', $item['id'])->select()[0]['name'];
            $item['already'] = count($choose->where('class_id', $item['id'])->select());
            $list[$key] = $item;
        }

        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function create()
    {
        $this->display();
    }

    public function store()
    {
        $class = new ClassModel();
        $data = I('post.');
        $data['teacher_id'] = $this->user['id'];
        $data['create_time'] = time();
        $data['update_time'] = time();
        $class->create($data);
        $result = $class->add();
        if (!$result) {
            $this->retError('新增失败');
        } else {
            $this->retSuccess();
        }
    }

    public function edit()
    {
        $class = (new ClassModel())->find(I('get.id'));
        $this->assign('class', $class);
        $this->display();
    }


    public function update()
    {
        $class = new ClassModel();
        $result = $class->save(I('post.'));
        if (!$result) {
            $this->retError('新增失败');
        } else {
            $this->retSuccess();
        }
    }

    public function delete()
    {
        $class = new ClassModel();
        $result = $class->where('id=' . I('post.id'))->delete();
        if (!$result) {
            $this->retError('新增失败');
        } else {
            $this->retSuccess();
        }
    }


    public function score()
    {
        $choose = M('ChooseClass'); // 实例化User对象
        $query = $choose->where('class_id', I('post.id'));

        $count = $query->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $query->order('create_time')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $user = new UserModel();
        foreach ($list as $key => $item) {
            $item['user'] = $user->where('id', $item['stu_id'])->select()[0]['name'];
            $list[$key] = $item;
        }
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function scoreConfirm()
    {

        $choose = M('ChooseClass'); // 实例化User对象
        $result = $choose->save(I('post.'));
        if (!$result) {
            $this->retError('打分失败');
        } else {
            $this->retSuccess();
        }
    }

    public function pgConfirm()
    {

        $choose = M('Task'); // 实例化User对象
        $result = $choose->save(I('post.'));
        if (!$result) {
            $this->retError('批改失败');
        } else {
            $this->retSuccess();
        }
    }


    public function task()
    {

        $choose = M('Task'); // 实例化User对象
        $query = $choose->where('class_id', I('get.id'));
        $count = $query->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $query->order('create_time')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $key => $item) {
            $class = (new ClassModel())->where('id', $item['class_id'])->select()[0];;
            $item['class'] = $class['name'];
            $list[$key] = $item;
        }
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->display(); // 输出模板

    }

    public function signin()
    {
        $model = new Model();
        $list = $model->query('select u.* from choose_class c,user u where u.id = c.stu_id and c.class_id = ' . I('get.id'));


        $s = new SigninModel(); // 实例化User对象
        foreach ($list as $key => $item) {
            $map['stu_id'] = $item['id'];
            $result = $s->where($map)->order('create_time', 'desc')->select();
            $isSign = 0;
            if ($result) {
                if (date('Y-m-d', $result[0]['create_time']) == date('Y-m-d', time())) {
                    $isSign = 1;
                }
            }

            $item['isSign'] = $isSign;
            $list[$key] = $item;
        }
        $this->assign('list', $list);// 赋值数据集

        $this->display(); // 输出模板

    }

}